package com.baizhi.controller;

import com.baizhi.exception.UserNotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

// @CrossOrigin    // 局部解决跨域
@RestController
public class DemoController {
    private static final Logger logger = LoggerFactory.getLogger(DemoController.class);

    @GetMapping("demo")
    public ResponseEntity<String> demo() {
        try {
            logger.debug("demo ok...");
            int n = 1 / 0;
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new ResponseEntity<>("demo ok", HttpStatus.OK);
    }

    @GetMapping("demo1")
    public ResponseEntity<String> demo1() {
        logger.debug("demo ok...");
        int n = 1 / 0;
        return new ResponseEntity<>("demo ok", HttpStatus.OK);
    }

    @RequestMapping("login")
    public ResponseEntity<String> login(String username, String password) {
        logger.debug("username：{}", username);
        logger.debug("password：{}", password);
        if ("xiaochen".equals(username) && "123".equals(password)) {
            return new ResponseEntity<>("登陆成功!!!", HttpStatus.OK);
        } else {
            throw new UserNotFoundException("用户名不正确!!!");
        }
    }
}
